package com.manpower.ui;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class EditUserInformationActivity extends BaseActivity{

	private View mParent;
	private TextView mUserName;
	private TextView mCountryName;
	private TextView mProvince;
	private TextView mTownship;
	private TextView mCommittee;
	private EditText mTeam;
	private TextView mHouseholdId;
	private TextView mHouseholdClass;
	private EditText mHouseholdNum;
	private EditText mMaleNum;
	private Button mCalNum;
	private EditText mFemaleNum;
	private EditText mUpSixteenNum;
	private EditText mMaleUpSixteenNum;
	private Button mCalUpSixteenNum;
	private EditText mFemaleUpSixteenNum;
	private EditText mLowHalfYearNum;
	private EditText mMaleLowHalfYearNum;
	private Button mCalLowHalfYearNum;
	private EditText mFemaleLowHalfYearNum;
	private EditText mUserCode;
	private EditText mPhoneNum;
	
	private int mPopDownItemSelected = 0;
	private PullDownList mPullDownList = null;
	private static final String NAME_TAG = "name";
	private static final String Detail_TAG = "detail";
	private static final String MARK_TAG = "mark";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_information_edit);
		initiate();
	}

	private void initiate() {
		txtTitle.setText(R.string.label_EnRegistration);
		mParent = (View)findViewById(R.id.informationParent);
		mUserName = (TextView)findViewById(R.id.username);
		mCountryName = (TextView)findViewById(R.id.country_name);
		mProvince = (TextView)findViewById(R.id.province);
		mTownship = (TextView)findViewById(R.id.township);
		mCommittee = (TextView)findViewById(R.id.committee);
		mTeam = (EditText)findViewById(R.id.team_input);
		mHouseholdId = (TextView)findViewById(R.id.househole_id);
		mHouseholdClass = (TextView)findViewById(R.id.household_class);
		mHouseholdNum = (EditText)findViewById(R.id.household_num);
		mMaleNum = (EditText)findViewById(R.id.male_num);
		mCalNum = (Button)findViewById(R.id.cal_num);
		mFemaleNum = (EditText)findViewById(R.id.female_num);
		mUpSixteenNum = (EditText)findViewById(R.id.num_up_sixteen);
		mMaleUpSixteenNum = (EditText)findViewById(R.id.male_num_up_sixteen);
		mCalUpSixteenNum = (Button)findViewById(R.id.cal_num_up_sixteen);
		mFemaleUpSixteenNum = (EditText)findViewById(R.id.female_num_up_sixteen);
		mLowHalfYearNum = (EditText)findViewById(R.id.num_low_halfyear);
		mMaleLowHalfYearNum = (EditText)findViewById(R.id.male_num_low_halfyear);
		mCalLowHalfYearNum =(Button)findViewById(R.id.cal_num_low_halfyear);
		mFemaleLowHalfYearNum = (EditText)findViewById(R.id.female_num_low_halfyear);
		mUserCode = (EditText)findViewById(R.id.user_code);
		mPhoneNum = (EditText)findViewById(R.id.phone_num);
		
		setClickListener();
	}
	
	private void setClickListener() {
		mHouseholdClass.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				if(mPullDownList==null){
					ArrayList<HashMap<String, Object>> popupAdapter = new ArrayList<HashMap<String, Object>>();
					HashMap<String, Object> curMap = new HashMap<String, Object>();
					
			        curMap.put(NAME_TAG, getString(R.string.pull_down_item_1).toString());
			        curMap.put(Detail_TAG,0);
			        curMap.put(MARK_TAG, 0);
			        popupAdapter.add(curMap);
			        HashMap<String, Object> curMap2 = new HashMap<String, Object>();
			        curMap2.put(NAME_TAG, getString(R.string.pull_down_item_2).toString());
			        curMap2.put(Detail_TAG, 0);
			        curMap2.put(MARK_TAG, 0);
			        popupAdapter.add(curMap2);
					mPullDownList = new PullDownList(EditUserInformationActivity.this, mParent, mHouseholdClass, popupAdapter);
				}
				int[] location = new int[2];
				EditUserInformationActivity.this.mHouseholdClass.getLocationOnScreen(location);
				mPullDownList.popupWindwShowing(location, mHouseholdClass.getHeight());
			}
		});
		mCalNum.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mHouseholdNum.getText().toString().length() > 0 && mMaleNum.getText().toString().length() > 0) {
					Log.d("errrrrrrr",(mHouseholdNum.getText().toString().length() + ""));
					int addOne = Integer.parseInt(mHouseholdNum.getText().toString());
					int addTwo = Integer.parseInt(mMaleNum.getText().toString());
					if(addOne >= addTwo) {
						mFemaleNum.setText(Integer.toString(addOne - addTwo));
					}
					else {
						Toast.makeText(EditUserInformationActivity.this, R.string.input_num_erro, Toast.LENGTH_SHORT).show();
					}
				}
				else {
					Toast.makeText(EditUserInformationActivity.this, R.string.input_num_erro, Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		mCalUpSixteenNum.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mUpSixteenNum.getText().toString().length() > 0 && mMaleUpSixteenNum.getText().toString().length() > 0) {
					int addOne = Integer.parseInt(mUpSixteenNum.getText().toString());
					int addTwo = Integer.parseInt(mMaleUpSixteenNum.getText().toString());
					if(addOne >= addTwo) {
						mFemaleUpSixteenNum.setText(Integer.toString(addOne - addTwo));
					}
					else {
						Toast.makeText(EditUserInformationActivity.this, R.string.input_num_erro, Toast.LENGTH_SHORT).show();
					}
				}
				else {
					Toast.makeText(EditUserInformationActivity.this, R.string.input_num_erro, Toast.LENGTH_SHORT).show();
				}
			}
		});
		mCalLowHalfYearNum.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mLowHalfYearNum.getText().toString().length() > 0 && mMaleLowHalfYearNum.getText().toString().length() > 0) {
					
					int addOne = Integer.parseInt(mLowHalfYearNum.getText().toString());
					int addTwo = Integer.parseInt(mMaleLowHalfYearNum.getText().toString());
					if(addOne >= addTwo) {
						mFemaleLowHalfYearNum.setText(Integer.toString(addOne - addTwo));
					}
					else {
						Toast.makeText(EditUserInformationActivity.this, R.string.input_num_erro, Toast.LENGTH_SHORT).show();
					}
				}
				else {
					Toast.makeText(EditUserInformationActivity.this, R.string.input_num_erro, Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	
	private void updateView() {
		
	}
	
	
    public class PullDownList { 
        
        private PopupWindow selectPopupWindow= null; 
        private View parentView = null; 
        private Activity activity = null; 
        private ListView listView = null; 
        private PullDownListAdapter pullListAdapter = null; 
//        private DeleteData deleteData = null; 
        ArrayList<HashMap<String, Object>> data = null; 
         
        private int[] location = new int[2]; 
        private int height; 
        private int width; 
         
        /**
         * 
         * @param _activity         调用此方法的Activity
         * @param _parentView       用来显示的PopupWindow的View
         * @param _ShowlocationView 基于那个控件显示
         * @param _data             数据源
         */ 
        public PullDownList(Activity _activity, View _parentView, View _ShowlocationView, ArrayList<HashMap<String, Object>> _data){ 
            activity = _activity; 
            parentView = _parentView; 
            _ShowlocationView.getLocationOnScreen(location); 
            width = _ShowlocationView.getWidth()*2; 
            height = _ShowlocationView.getHeight(); 
            data = _data; 
            initPopuWindow(); 
        } 
         
        /**
         * 初始化PopupWindow
         */ 
        private void initPopuWindow(){ 
            View loginwindow = (View) activity.getLayoutInflater().inflate(R.layout.popupwindow_deal_palce_pull_down, null); 
            listView = (ListView) loginwindow.findViewById(R.id.popupwindow_pulldown_list); 
            pullListAdapter = new PullDownListAdapter(activity, data); 
            listView.setAdapter(pullListAdapter); 
            selectPopupWindow = new PopupWindow(loginwindow, mHouseholdClass.getWidth(), LayoutParams.WRAP_CONTENT, true);
//            selectPopupWindow = new PopupWindow(loginwindow, mHouseholdClass.getWidth(), 
//            		EditUserInformationActivity.this.getResources().getDimensionPixelSize(R.dimen.popup_item_height), true); 
            selectPopupWindow.setOutsideTouchable(true); 
            selectPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            listView.setOnItemClickListener(new OnItemClickListener() { 
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {            
                	mPopDownItemSelected = arg2;
                	if(arg2==0){
                		mHouseholdClass.setText(R.string.pull_down_item_1);
                	} else {
                		mHouseholdClass.setText(R.string.pull_down_item_2);
                	}
                	
                    selectPopupWindow.dismiss(); 
                } 
            }); 
        } 
         
        /**
         * 显示PopupWindow窗口
         * 
         * @param popupwindow
         */ 
        public void popupWindwShowing(int[] location, int height) { 
             
            selectPopupWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, location[0], location[1] + height); 
//        	selectPopupWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, 50, 300); 
        } 
         
        /**
         * PopupWindow消失
         */ 
        public void dismiss(){ 
            selectPopupWindow.dismiss(); 
        } 
         
         
        class PullDownListAdapter extends BaseAdapter { 
            private ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); 
            private Activity activity = null; 
     
            public PullDownListAdapter(Activity _activity, 
                    ArrayList<HashMap<String, Object>> _list) { 
                activity = _activity; 
                list = _list; 
            } 
     
            public int getCount() { 
                return list.size(); 
            } 
     
            public Object getItem(int position) { 
                return list.get(position); 
            } 
     
            public long getItemId(int position) { 
                return position; 
            } 
     
            class ViewHolder { 
                TextView textView; 
                TextView textViewSecond; 
                ImageView imageView; 
            } 
             
            private void removeItem(int position){ 
                list.remove(position); 
                notifyDataSetChanged(); 
            }

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				 
                ViewHolder holder = null; 
                if (convertView == null) { 
                    holder = new ViewHolder(); 
                    convertView = LayoutInflater.from(activity).inflate( 
                            R.layout.pull_down_item, null); 
                    holder.textView = (TextView) convertView 
                            .findViewById(R.id.pull_list_item_text); 
//                    holder.textViewSecond = (TextView) convertView 
//                            .findViewById(R.id.pull_list_item_text_second); 
//                    holder.imageView = (ImageView) convertView 
//                            .findViewById(R.id.pull_list_item_image); 
                    convertView.setTag(holder); 
                } else { 
                    holder = (ViewHolder) convertView.getTag(); 
                } 
                holder.textView.setText((CharSequence) list.get(position).get(NAME_TAG)); 
//                holder.textViewSecond.setText((CharSequence) list.get(position).get(Detail_TAG)); 
                if(position==0){
                    convertView.setBackgroundResource(R.drawable.popup_list_style);
                }else{
                    convertView.setBackgroundResource(R.drawable.popup_list_style);
                } 
                return convertView; 
            
			} 
        } 
    } 
}
